<!--
 * @Author: fanyaqing
 * @Date: 2021-08-05 08:34:01
 * @LastEditTime: 2021-08-12 10:52:41
 * @LastEditors: fanyaqing
 * @Description: 
 * @FilePath: \supermall\src\views\home\home.vue
-->
<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control
      ref="tabControlFixed"
      class="tab-control"
      :title="['流行','新款','精选']"
      @tabClick="tabClick"
      v-show="isTabFixed"
    ></tab-control>
    <scroll class="content"
      ref="scroll"
      :probeType="3"
      pullUpLoad
      @scrollPosition="scrollPosition"
      @pullingUp="loadMore"
    >
      <home-swiper
        :banners="banners"
        @homeSwiperImgLoaded="homeSwiperImgLoaded"
      ></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control
        ref="tabControl"
        :title="['流行','新款','精选']"
        @tabClick="tabClick"
      ></tab-control>
      <goods-list :goods="showGoods"></goods-list>
     
    </scroll>
    <back-top v-show="backTopShow" @click.native="backClick"></back-top>
  </div>
</template>
<script>
import Scroll from 'components/common/scroll/Scroll'
import NavBar from 'components/common/navbar/NavBar';
import TabControl from 'components/content/tabControl/TabControl'
import GoodsList from 'components/content/goods/GoodsList'

import HomeSwiper from './childComponents/HomeSwiper';
import RecommendView from './childComponents/RecommendView';
import FeatureView from './childComponents/FeatureView';

import placeholder from 'assets/img/common/placeholder.png';
import placeholderImgPop from 'assets/img/common/placeholder_pop.png';
import placeholderImgNew from 'assets/img/common/placeholder_new.png';
import placeholderImgSell from 'assets/img/common/placeholder_sell.png';

import {debounce} from 'common/utils'
import {itemListenerMixin,backTopMixin} from 'common/mixin'

import {getHomeMultidata,getHomeGoods} from 'network/home'

export default {
  components:{
    Scroll,
    NavBar,
    TabControl,
    GoodsList,
    HomeSwiper,
    RecommendView,
    FeatureView
  },
  mixins:[itemListenerMixin,backTopMixin],
  data() {
    return {
      banners:[],
      recommends:[],
      goods:{
        'pop':{page:0,list:[
          {show:{img:placeholderImgPop},id:'id1',price:'10.00',cfav:20,title:'这是一个商品的标题。。。。。。。。。。。。。。。。'},
          {show:{img:placeholderImgPop},id:'id2',price:'10.00',cfav:20,title:'这是一个商品的标题。'},
          {show:{img:placeholderImgPop},id:'id3',price:'10.00',cfav:20,title:'这是一个商品的标题。'},
          {show:{img:placeholderImgPop},id:'id4',price:'10.00',cfav:20,title:'这是一个商品的标题。'},
          {show:{img:placeholderImgPop},id:'id5',price:'10.00',cfav:20,title:'这是一个商品的标题。'},
          {show:{img:placeholderImgPop},id:'id6',price:'10.00',cfav:20,title:'这是一个商品的标题。'},

        ]},
        'new':{page:0,list:[
          {show:{img:placeholderImgNew},id:'id1',price:'10.00',cfav:20,title:'这是一个商品的标题。。。。。。。。。。。。。。。。'},
          {show:{img:placeholderImgNew},id:'id2',price:'10.00',cfav:20,title:'这是一个商品的标题。'},
          {show:{img:placeholderImgNew},id:'id3',price:'10.00',cfav:20,title:'这是一个商品的标题。'},
        ]},
        'sell':{page:0,list:[
          {show:{img:placeholderImgSell},id:'id1',price:'10.00',cfav:20,title:'这是一个商品的标题。。。。。。。。。。。。。。。。'},
          {show:{img:placeholderImgSell},id:'id2',price:'10.00',cfav:20,title:'这是一个商品的标题。'},
          {show:{img:placeholderImgSell},id:'id3',price:'10.00',cfav:20,title:'这是一个商品的标题。'},
        ]},
      },
      tempArray:[
        {show:{img:placeholder},id:'id1',price:'100.00',cfav:20,title:'这是一个商品的标题。'},
        {show:{img:placeholder},id:'id2',price:'100.00',cfav:20,title:'这是一个商品的标题。'},
        {show:{img:placeholder},id:'id3',price:'100.00',cfav:20,title:'这是一个商品的标题。'},
      ],
      currentType:'pop',
      homeSwiperOffsetTop:0,
      isTabFixed:false,
      saveY:0,
    }
  },
  computed:{
    showGoods(){
      return this.goods[this.currentType].list
    }
  },
  created(){
    // 1.请求多个数据
    getHomeMultidata().then(res=>{
      this.banners=res.data.banner.list;
      this.recommends=res.data.recommend.list;
    })
    // 2.请求各个商品的数据，分别加入到goods对象中
    // this.getHomeGoods('pop');
    // this.getHomeGoods('new');
    // this.getHomeGoods('sell');
  },
  mounted(){
    // 取refs的时候不要在created中，
    // 防抖处理，本来需要频繁执行多次的方法，只在最后执行一次就行
    // 下面代码和详情页的代码冗余，进行混入处理
    // const refresh=debounce(this.$refs.scroll.refresh,200)
    // this.itemImgListener=()=>{
    //   refresh();
    // }
    // this.$bus.$on('imageLoaded',this.itemImgListener)
    
  },
  activated(){
    this.$refs.scroll.refresh()

    this.$refs.scroll.scrollTop(0,this.saveY,0)
  },
  deactivated(){
    this.saveY=this.$refs.scroll.getScrollY()
    // console.log(this.saveY)
    // 当前组件关闭时，将事件总线销毁，防止和其他同名的事件总线相互影响
    this.$bus.$off('imageLoaded',this.itemImgListener)
  },
  methods:{
    /*
    网络请求接口相关
    */ 
    getHomeGoods(type){
      const page=this.goods[type].page+1;
      // 接口数据
      // getHomeGoods(type,page).then(res=>{
      //   this.goods[type].list.push(...res.data.list);//将[a,b,c],里面的元素，加入到goods对应的类型数据中；
      //   this.goods[type].page+=1;
      // })
      // 模拟数据
      this.goods[this.currentType].list.push(...this.tempArray)
      // 完成加载更多
      this.$refs.scroll.finish();

    },
    /*
    事件监听相关
    */ 
    tabClick(index){
      switch (index) {
        case 0:
          this.currentType='pop'
          break;
        case 1:
          this.currentType='new'
          break;
        case 2:
          this.currentType='sell'
          break;  
      }
      this.$refs.tabControlFixed.currentIndex=index;
      this.$refs.tabControl.currentIndex=index;
    },
    scrollPosition(position){
      this.listenShowBackTop(position);
      this.isTabFixed=Math.abs(position.y)>this.homeSwiperOffsetTop;
    },
    
    loadMore(){
      this.getHomeGoods(this.currentType);
      console.log('加载更多')
    },
    homeSwiperImgLoaded(){
      // 获取tabcontrol的offsetTop，来实现吸顶效果
      // tabControl组件，本身没有，offsetTop，需要永$el找到组件内部的元素来获取offsetTop
      // 直接获取的offsetTop，数值不对，因为上面的有些图片可能没加载出来，图片的高度没算上，要图片加载完成后再执行这个方法
      this.homeSwiperOffsetTop=this.$refs.tabControl.$el.offsetTop;
    }
    
  }
} 
</script>
<style scoped>
/* 
  在content设置某个固定高度时，better-scroll就成功实现，
  为了要在整个页面视图上进行滚动，需要对content高度进行一些设置。
  方法一：
    #home的height，设置为100vh，就时当前视图的高度
    centent的height，设置为calc(100%-44头部高度-49尾部的高度)
    这个好像有问题=>calc的减号旁边要有空格！！
  方法二：（推荐）
    #home 的视框高度设置为100vh，
    content，使用position绝对定位，top：44，bottom：49，
*/
  #home{
    padding-top: 44px;
    height: 100vh;

    position: relative;
  }
  .content{
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
  /* .content{
    height: calc(100% - 93px);
    overflow: hidden;
    margin-top: 44px;
  } */
  .home-nav{
    background-color: var(--color-tint);
    color: #ffffff;

    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 5;
  }
  .tab-control{
    position: relative;
    z-index: 9;
  }
</style>